/**
 * Created by gongxun on 16/11/21.
 */

var KwxPlayerMyModel = function(){
    var that = new KwxPlayerModel();

    //选中牌的index
    that.selectInhandCardIndex = null;

    //滑动出牌的牌
    that.selectMoveCard = null;

    //滑动出牌的牌是否显示了
    that.isSelectMoveCardShowOnce = null;

    //自己不能出的牌 (别人听的牌)
    that.selfNotPlayCardNums = [];

    //自己听的牌
    that.selfTingCardNums = [];

    //自己亮听的时候黑掉的牌
    that.selfTingOperatingBlackNums = [];

    //flag
    that.mFlagWeixianNums = [];
    that.mFlagLiangNums = [];
    that.mFlagTingNums = [];
    that.mFlagKouNums = [];

    //染色
    that.cardColorOtherTing = cc.color(160, 160, 160);
    that.cardColorLiang = cc.color(160, 160, 160);
    that.cardColorSelectMove = cc.color(230, 230, 230);
    that.cardColorNotPlaySeat = cc.color(210, 210, 210);

    //监听手牌触摸
    that.loadTouchListener = function(node){
        cc.eventManager.addListener({
            event: cc.EventListener.TOUCH_ONE_BY_ONE,
            swallowTouches: true,
            onTouchBegan: function(touch, event){
                //抬起或者打出手牌
                var location = touch.getLocation();
                var point = mo.gameMainLayer.getTransfromPoint(location);
                return that.onTouchCardBegan(point);
            },
            onTouchMoved: function(touch, event){
                var location = touch.getLocation();
                var point = mo.gameMainLayer.getTransfromPoint(location);
                that.moveSelectMoveCard(point);
            },
            onTouchEnded: function(touch, event){
                var location = touch.getLocation();
                var point = mo.gameMainLayer.getTransfromPoint(location);
                that.endSelectMoveCard(point);
            },
            onTouchCancelled : function(touch, event){
                var location = touch.getLocation();
                var point = mo.gameMainLayer.getTransfromPoint(location);
                that.setSelectMoveCard(false);
            }
        }, node);
    };

    //点牌触发
    that.onTouchCardBegan = function(point){

        point = mo.gameHelper.getPointForNode(mo.sceneMainLayer, point);

        //设置手牌位置
        that.setInahndCardPosition();

        //手牌大小
        var cardSize = mo.kwxCardHelper.getInhnadCardSize();

        for (var i = 0; i < that.inhandCards.length; i++){
            //手牌位置
            var card = that.inhandCards[i];
            var cardPos = mo.kwxCardHelper.getInhandCardPos(that.seat, i, that.openCards.length, (that.isFromCard && (i == that.inhandCards.length - 1)));

            var rect = cc.rect(cardPos.x - cardSize.width / 2, cardPos.y, cardSize.width, cardSize.height);

            //是否点中手牌
            if (cc.rectContainsPoint(rect, point)){
                if (card.mIsColor){
                    //染色了就不响应触摸
                    break;
                }

                if (that.setSelectInhandCard(i)){
                    mo.kwxTableStatus.sendPlayCard(card.mCardNum);
                    return false;
                }else {
                    that.setSelectMoveCard(true, card);
                    return true;
                }
            }
        }

        that.setSelectInhandCard(null);

        return false;
    };

    //设置选中牌 返回true 需要打出牌 返回false启动滑动出牌
    that.setSelectInhandCard = function(index){
        if (index != null){
            //选中了牌
            if (index == that.selectInhandCardIndex){
                return true;
            }else{
                that.selectInhandCardIndex = index;
                mo.soundHelper.playMjSelect();
            }
        }else{
            //没有选 取消所有牌的选择
            that.selectInhandCardIndex = null;
        }

        //亮听的时候通知ui显示可以胡哪些牌
        if (mo.kwxTableStatus.useTing){
            var cardNum = null;
            if (that.selectInhandCardIndex != null){
                cardNum = that.inhandCards[that.selectInhandCardIndex].mCardNum;
            }
            mo.kwxTable.mKwxTableUILayer.mKwxTableOperatingLayer.getTingTimeHuCardNum(cardNum);
        }else {
            //有需求可以亮听 不管亮不亮 都显示出来
            var cardNum = null;
            if (that.selectInhandCardIndex != null){
                cardNum = that.inhandCards[that.selectInhandCardIndex].mCardNum;
            }

            if (!mo.kwxTableStatus.mTableStatus["SeatUser" + mo.kwxTableStatus.mTableStatus.MySeat].OpenTing){
                //自己没有亮才有这个操作
                if (cardNum == null){
                    mo.kwxTable.mKwxTableUILayer.setMyShowTing([]);
                }else{
                    var canHuCardNums = mo.kwxTableStatus.getMyHuNums(cardNum);
                    canHuCardNums.sort();
                    mo.kwxTable.mKwxTableUILayer.setMyShowTing(canHuCardNums);
                }
            }
        }

        //牌位置
        that.setInahndCardPosition();

        return false;
    };

    that.setSelectMoveCard = function(isSelect, card){
        if (isSelect){
            that.selectMoveCard = mo.kwxTable.mKwxCardLayer.getSelectMoveSprite();
            that.selectMoveCard.setCardNum(card.mCardNum);
            that.selectMoveCard.setCardKind(0, 1);
            that.selectMoveCard.setVisible(false);
            card.setCardColor(that.cardColorSelectMove);
            that.isSelectMoveCardShowOnce = false;
        }else{
            if (that.selectMoveCard){
                that.selectMoveCard.setVisible(false);
            }
            that.startSetInhandCardColor();
            that.selectMoveCard = null;

            if (that.isSelectMoveCardShowOnce){
                that.setSelectInhandCard(null);
            }
        }
    };

    that.moveSelectMoveCard = function(point){
        var selectCardSize = mo.kwxCardHelper.getInhnadCardSize();
        var showPoint = cc.p(point.x - selectCardSize.width / 2, point.y - selectCardSize.height / 2);
        that.selectMoveCard.setPosition(showPoint);
        if (point.y > mo.kwxCardHelper.getInhnadCardSize().height - 10){
            that.selectMoveCard.setVisible(true);
            that.isSelectMoveCardShowOnce = true;
        }else{
            that.selectMoveCard.setVisible(false);
        }
    };

    that.endSelectMoveCard = function(point){
        if (point.y > mo.kwxCardHelper.getInhnadCardSize().height - 10){
            mo.kwxTableStatus.sendPlayCard(that.selectMoveCard.mCardNum);
        }
        that.setSelectMoveCard(false)
    };

    // //强制排序
    // that.sortInahndCard = function(){
    //     if (that.inhandCards.length == 0){
    //         return;
    //     }
    //
    //     mo.kwxCardHelper.sortCardForNum(that.inhandCards);
    //     for (var i = 0; i < that.inhandCards.length; i++){
    //         var card = that.inhandCards[i];
    //         card.stopAllActions();
    //
    //         var p = mo.kwxCardHelper.getInhandCardPos(that.seat, i, that.openCards.length, (that.isFromCard && (i == that.inhandCards.length - 1)));
    //         if (i == that.selectInhandCardIndex){
    //             p.y += 30;
    //         }
    //
    //         card.setRotation(0);
    //         card.setPosition(p);
    //     }
    // };

    //设置手牌位置 不排序
    that.setInahndCardPosition = function(){
        for (var i = 0; i < that.inhandCards.length; i++){
            var card = that.inhandCards[i];
            card.stopAllActions();
            card.setOpacity(255);
            var p = mo.kwxCardHelper.getInhandCardPos(that.seat, i, that.openCards.length, (that.isFromCard && (i == that.inhandCards.length - 1)));
            if (i == that.selectInhandCardIndex){
                p.y += 30;
            }

            card.setRotation(0);
            card.setPosition(p);
        }
    };

    //手牌染色 唯一染色 有多少个染多少个 不染其他num一样的牌
    that.setInhandCardColor = function(grayCardNums, yellowCardNums){
        var useIndexs = [];
        for (var i = 0; i < that.inhandCards.length; i++){
            that.inhandCards[i].resetCardColor();
        }

        for (var i = 0; i < grayCardNums.length; i++){
            var index = mo.kwxCardHelper.findCardForNumApartIndexs(that.inhandCards, grayCardNums[i], useIndexs);
            if (index != null){
                that.inhandCards[index].setCardColor(that.cardColorOtherTing);
                useIndexs.push(index);
            }
        }

        useIndexs = [];
        for (var i = 0; i < yellowCardNums.length; i++){
            var index = mo.kwxCardHelper.findCardForNumApartIndexs(that.inhandCards, yellowCardNums[i], useIndexs);
            if (index != null){
                that.inhandCards[index].setCardColor(that.cardColorLiang);
                useIndexs.push(index);
            }
        }

        //如果自己没有听 且不是亮牌最后阶段 且不失扣牌阶段 而且所有手牌都染色了  就取消所有手牌的染色
        var selfSeat = mo.kwxTableStatus.mTableStatus["SeatUser" + mo.kwxTableStatus.mTableStatus.MySeat];
        if (selfSeat){
            if (!mo.kwxTableStatus.useKouTing){
                if (!selfSeat.OpenTing && mo.kwxTableStatus.useTing == false){
                    var isAllBlack = true;
                    for (var i = 0; i < that.inhandCards.length; i++){
                        if (!that.inhandCards[i].mIsColor){
                            isAllBlack = false;
                            break;
                        }
                    }

                    if (isAllBlack){
                        that.setInhandCardColor([], []);
                    }
                }
            }
        }

        //如果不该自己出牌 就把所有没有染色的手牌染色
        if (!mo.kwxTableStatus.getMyIsPlayCard()){
            for (var i = 0; i < that.inhandCards.length; i++){
                if (!that.inhandCards[i].mIsColor) {
                    that.inhandCards[i].setCardColor(that.cardColorNotPlaySeat);
                }
            }
        }
    };

    //开始手牌染色
    that.startSetInhandCardColor = function(){
        //找到手牌中所有的
        var grayCardNums = [];
        var findGrayNums = [];
        if (mo.kwxTableStatus.useKouTing){
            //扣牌时候
            findGrayNums = that.selfTingOperatingBlackNums;
        }else{
            if (mo.kwxTableStatus.useTing){
                findGrayNums = that.selfTingOperatingBlackNums.concat(that.selfNotPlayCardNums);
            }else{
                findGrayNums = that.selfNotPlayCardNums;
            }
        }
        for (var i = 0; i < that.inhandCards.length; i++){
            var num = that.inhandCards[i].mCardNum;
            var find = mo.gameHelper.findValueForArray(findGrayNums, num);
            if (find != null){
                grayCardNums.push(num);
            }
        }

        //有都少个找多少个
        var yellowCardNums = that.selfTingCardNums;

        that.setInhandCardColor(grayCardNums, yellowCardNums);

        return grayCardNums;
    };

    //黑掉指定的牌 听牌操作时候专用
    that.setTingOperatingBlack = function(blackNums){
        that.selfTingOperatingBlackNums = blackNums;

        return that.startSetInhandCardColor();
    };

    //设置手牌的flag weixianNums全展示 liangNums展示固定数量
    that.setInhandCardFlag = function(){
        var weixianNums = that.mFlagWeixianNums;
        var liangNums = that.mFlagLiangNums;
        var tingNums = that.mFlagTingNums;
        var kouNums = that.mFlagKouNums;

        //重置一遍
        for (var i = 0; i < that.inhandCards.length; i++){
            that.inhandCards[i].setFlag(-1);
        }

        //危险牌
        for (var i = 0; i < that.inhandCards.length; i++){
            if (mo.gameHelper.findValueForArray(weixianNums, that.inhandCards[i].mCardNum) != null){
                // that.inhandCards[i].setFlag(0);
            }
        }

        //扣的牌
        mo.kwxTable.mKwxCardLayer.setKouCard(0, kouNums);

        //听的牌
        for (var i = 0; i < that.inhandCards.length; i++){
            if (mo.gameHelper.findValueForArray(tingNums, that.inhandCards[i].mCardNum) != null){
                that.inhandCards[i].setFlag(2);
            }
        }

        //亮的牌
        var useLiangIndexs = [];
        for (var i = 0; i < liangNums.length; i++){
            var index = mo.kwxCardHelper.findCardForNumApartIndexs(that.inhandCards, liangNums[i], useLiangIndexs);
            if (index != null){
                // that.inhandCards[index].setFlag(1);
                useLiangIndexs.push(index);
            }
        }
    };

    //重新排序听的牌
    that.sortTingCardAndTurn = function(liangNums){
        //重置
        for(var i = 0; i < that.inhandCards.length; i++){
            that.inhandCards[i].mIsLiangCard = false;
        }

        //翻面
        var useLiangIndexs = [];
        for (var i = 0; i < liangNums.length; i++){
            var index = mo.kwxCardHelper.findCardForNumApartIndexs(that.inhandCards, liangNums[i], useLiangIndexs);
            if (index != null){
                useLiangIndexs.push(index);

                var card = that.inhandCards[index];
                var color = card.getColor();
                card.setCardKind(0, 3);
                card.setScale(mo.kwxCardHelper.inhandCardScale);
                card.setCardColor(color);
                card.mIsLiangCard = true;
            }
        }

        //排序
        if (that.inhandCards.length > 0 && that.isFromCard){
            var lastCard = that.inhandCards.pop();
            mo.kwxCardHelper.sortCardForNumArray(that.inhandCards);
            that.inhandCards.push(lastCard);
        }else{
            mo.kwxCardHelper.sortCardForNumArray(that.inhandCards);
        }

        for (var i = 0; i < that.inhandCards.length; i++){
            var card = that.inhandCards[i];
            var p = mo.kwxCardHelper.getInhandCardPos(that.seat, i, that.openCards.length, (that.isFromCard && (i == that.inhandCards.length - 1)));
            card.setPosition(p);
        }
    };

    return that;
};